
var activityId;		//活动id
var activityName;  //活动名称
var productlist=[];   //活动商品

var status;   //活动状态   未开始，进行中，已结束

apiready = function(){

  status=api.pageParam.status;
  activityId=api.pageParam.activityId;
  activityName=api.pageParam.activityName;
  
  api.setRefreshHeaderInfo({
        visible: true,
        // loadingImgae: 'wgt://image/refresh-white.png',
        bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function (ret, err) {
        showData();
        
        api.refreshHeaderLoadDone();
    });
    
    //添加活动状态改变事件
	api.addEventListener({
	    name: 'changeactivitystatus'
	}, function(ret){
	    if(ret){
	       status=ret.value;  //状态
	       showData();
	    }
	});
    
    
 
    showData();

};

//活动商品
function showData(){
     
     
     do_ajax('/app/activity/info?name='+activityName,'get',{},function(ret,err){
       
      
       if(ret){
           productlist =ret.prolist;
           
           var dataHtml = '';
           if(productlist.length>0){
               for(var i=0;i<productlist.length;i++){
                   dataHtml += '<div class="am-u-sm-12" style="border-bottom: 1px solid #efefef;padding:0px">';
	                   dataHtml += '<div class="am-u-sm-3" style="padding: 10px;" onclick="productinfo('+i+')">';
	                   dataHtml += '<img src="'+serviceUrl+productlist[i].proDefImg+'" style="height: 60px"/>';
	                   dataHtml += '</div>';
	                   dataHtml += '<div class="am-us-sm-9" style="padding: 5px 10px">';
		                   dataHtml += '<div class="am-text-truncate" onclick="productinfo('+i+')">';
		                   dataHtml += productlist[i].proName;
		                   dataHtml += '</div>';
			               dataHtml += '<div style="overflow: hidden">';
			               dataHtml += '<span  style="float: left">';
			               dataHtml += '<span style="color:red">￥'+productlist[i].actprice+'</span>';
			               dataHtml += '<s style="color:#aeaeae;font-size: 13px;">￥'+productlist[i].price+'</s>';
			               dataHtml += '</span>';
			               
			               if(status=='未开始'){
			                   dataHtml += '<span style="float:right;padding:2px 8px;text-align:center;border-radius: 5px;background:#aeaeae;color:#FFFFFF">未开始</span>';
			               }else if(status=='已结束'){
			                   dataHtml += '<span style="float:right;padding:2px 8px;text-align:center;border-radius: 5px;background:#aeaeae;color:#FFFFFF">已结束</span>';
			               }else{
								if(productlist[i].maxCount <= productlist[i].saleCount){
									dataHtml += '<span style="float:right;padding:2px 8px;text-align:center;border-radius: 5px;background:#aeaeae;color:#FFFFFF">已抢光</span>';
								}else{
									dataHtml += '<span onclick="grabbuy('+i+')" style="float:right;padding:2px 8px;text-align:center;border-radius: 5px;background: #ffd1d3;color:#fc353a">马上抢</span>';
								}
			               }
			               
			               dataHtml += '</div>';
			               dataHtml += '<div style="font-size:15px">';
			               dataHtml += '<span style="color: #aeaeae">共'+productlist[i].maxCount+'件</span>';
			               dataHtml += '<span style="color: #aeaeae;float: right">已抢购'+productlist[i].saleCount+'件</span>';
			               dataHtml += '</div>';
	                 dataHtml += '</div>';
	                 
	               dataHtml += '</div>';
               
               }
               
               
               
           }else{
               dataHtml += '<div  class="am-u-sm-12" style="margin-top:20px;text-align:center">暂无数据...</div>'
           }
           
           
           $api.html($api.byId('productlist'), dataHtml);
           
           
           
           
       }else{
            api.alert({
	            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
	        });
       
       }
        api.hideProgress();
   });
     
     
};


//商品详情
function productinfo(index){

    
    do_ajax('/app/productshow?id='+productlist[index].proId,'get',{},function(ret,err){
       if(ret){
            api.openWin({
			    name: 'productinfo',
			    url: '../shop/productinfo.html',
			    pageParam: {product:ret,source:'抢购'}
		    });
          
          
       }else{
           api.alert({
	            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
	        });
       
       }
       
       api.hideProgress();
       
       
       
    });
       
       

    
    
    
};



//抢购
function grabbuy(index){
    
    //先检查登录
    var account = $api.getStorage('account');
    if (!account) {
         api.openWin({
	        name: 'login',
	        url: '../shop/login.html'
	     });
	     
	     return;
    }
	    
    var product={
         '_id':productlist[index].proId,
         'num':1,   //购买数量
         'name':productlist[index].proName,
         'proDefImg':productlist[index].proDefImg,
         'price':productlist[index].actprice,   //活动价格
         'merName':'',
         'code':'',
         'purprice':productlist[index].price,
    };

    api.openWin({
	    name: 'buy',
	    url: '../shop/buy.html',
	    pageParam: {product:product,source:'抢购',activityId:activityId}
    });
    
    
};





 





